package com.dongdongshop.controller;


import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.admin.vo.GoodsDescVO;
import com.dongdongshop.admin.vo.GoodsVO;
import com.dongdongshop.admin.vo.ItemVO;
import com.dongdongshop.common.Result;
import com.dongdongshop.dto.SellerDTO;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private JwtUtil jwtUtil;
    @PostMapping("insertInfo")
    public Result insertInfo(GoodsVO goodsVO, @RequestHeader("AutharizeToken") String AutharizeToken, GoodsDescVO goodsDescVO,String str){
        if (goodsVO == null){
            return Result.Response(ResultEnum.ERROR);
        }
        try {
            Claims claims = jwtUtil.parseJwt(AutharizeToken);
            SellerDTO sellerDTO = JSONObject.parseObject(claims.getSubject(), SellerDTO.class);
            List<ItemVO> itemlist = JSONObject.parseArray(str, ItemVO.class);
            //取出sellerId
            goodsVO.setSellerId(sellerDTO.getUname());
            //设置商品审核状态 默认为0
            goodsVO.setAuditStatus("0");
            goodsService.insertInfo(goodsVO,goodsDescVO,itemlist);
            return Result.OK();
        }catch (Exception e) {
            System.out.println(e);
            return Result.Response(ResultEnum.ERROR);
        }
    }

    @PostMapping("listGoods")
    public Result getAll(String goodsName){
        List<GoodsVO> list = goodsService.getAll(goodsName);
        return Result.OK().setData(list);
    }

    @PostMapping("auditYes")
    public Result auditYes(Long[] ids){
        GoodsVO goodsVO = goodsService.auditYes(ids);
        return Result.OK();
    }

    @PostMapping("auditNo")
    public Result auditNo(Long[] ids){
        GoodsVO goodsVO = goodsService.auditNo(ids);
        return Result.OK();
    }

    @PostMapping("getById")
    public Result getById(Long id){
        GoodsVO goodsVO = goodsService.getById(id);
        return Result.OK().setData(goodsVO);
    }
}
